var HellaFoxPrefs = {
    verifyPrefs: function() {
        var hostname = document.getElementById("hostnamePref").value;
        var port = document.getElementById("portPref").value;
        var password = document.getElementById("passwordPref").value;
        var status_delay = document.getElementById("statusDelayPref").value;

        if (!checkHostname(hostname)) {
            var prop_msg = hellafoxPrefsBundle.getString("invalidCharsInHostname");
            alert(prop_msg);
            return false;
        }
        if (!checkPort(port)) {
            var prop_msg = hellafoxPrefsBundle.getString("invalidCharsInPort");
            alert(prop_msg);
            return false;
        }
        if (!checkPassword(password)) {
            var prop_msg = hellafoxPrefsBundle.getString("invalidPassword");
            alert(prop_msg);
            return false;
        }
        if (!checkStatusDelay(status_delay)) {
            var prop_msg = hellafoxPrefsBundle.getString("invalidCharsInStatusDelay");
            alert(prop_msg);
            return false;
        }
        return true;
    },

    doOK: function() {
        var hostname = document.getElementById("hostnamePref").value;
        var port = document.getElementById("portPref").value;
        var password = document.getElementById("passwordPref").value;
        var status_delay = document.getElementById("statusDelayPref").value;

        HellaFoxPrefs.setHostname(hostname);
        HellaFoxPrefs.setPort(port);
        HellaFoxPrefs.setPassword(password);
        HellaFoxPrefs.setStatusDelay(status_delay);
        return true;
    },

    checkHostname: function(hostname) {
        var regex = /^[A-Za-z0-9_\-\.]+$/; 
        return regex.test(hostname);
    },

    checkPort: function(port) {
        var regex = /^[0-9]+$/; 
        return regex.test(port);
    },

    checkStatusDelay: function(status_delay) {
        var regex = /^[0-9]+$/; 
        return regex.test(status_delay);
    },

    checkPassword: function(password) {
        var regex = /^\s*$/;
        return regex.test(password);
    },

    getHostname: function() {
        var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                            .getService(Components.interfaces.nsIPrefBranch);

        if(prefs.getPrefType("hellafox.hostname") == prefs.PREF_STRING) {
            return prefs.getCharPref("hellafox.hostname");
        }
        else {
            prefs.setCharPref("hellafox.hostname", "localhost");
            return prefs.getCharPref("hellafox.hostname");
        }
    },

    setHostname: function(hostname) {
        var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                            .getService(Components.interfaces.nsIPrefBranch);

        return prefs.setCharPref("hellafox.hostname", hostname);
    },


    getPort: function() {
        var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                            .getService(Components.interfaces.nsIPrefBranch);

        if(prefs.getPrefType("hellafox.port") == prefs.PREF_INT) {
            return prefs.getIntPref("hellafox.port");
        }
        else {
            prefs.setIntPref("hellafox.port", 8760);
            return prefs.getIntPref("hellafox.port");
        }
    },

    setPort: function(port) {
        var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                            .getService(Components.interfaces.nsIPrefBranch);

        return prefs.setIntPref("hellafox.port", port);
    },

    getPassword: function() {
        var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                            .getService(Components.interfaces.nsIPrefBranch);

        if(prefs.getPrefType("hellafox.password") == prefs.PREF_STRING) {
            return prefs.getCharPref("hellafox.password");
        }
        else {
            prefs.setCharPref("hellafox.password", "changeme");
            return prefs.getCharPref("hellafox.password");
        }
    },

    setPassword: function(password) {
        var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                            .getService(Components.interfaces.nsIPrefBranch);

        return prefs.setCharPref("hellafox.password", password);
    },

    getStatusDelay: function() {
        var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                            .getService(Components.interfaces.nsIPrefBranch);

        if(prefs.getPrefType("hellafox.status_delay") == prefs.PREF_INT) {
            return prefs.getIntPref("hellafox.status_delay");
        }
        else {
            prefs.setIntPref("hellafox.status_delay", 3);
            return prefs.getIntPref("hellafox.status_delay");
        }
    },

    setStatusDelay: function(status_delay) {
        var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                            .getService(Components.interfaces.nsIPrefBranch);

        return prefs.setIntPref("hellafox.status_delay", status_delay);
    },

    onLoad: function() {
        hellafoxPrefsBundle = document.getElementById("bundle_hellafox_prefs");
        document.getElementById("hostnamePref").value = HellaFoxPrefs.getHostname();
        document.getElementById("portPref").value = HellaFoxPrefs.getPort();
        document.getElementById("passwordPref").value = HellaFoxPrefs.getPassword();
        document.getElementById("statusDelayPref").value = HellaFoxPrefs.getStatusDelay();
    }
};

var hellafoxPrefsBundle;
